package com.qinker.oa.controller;

import com.qinker.oa.model.CustomerContactMessage;
import com.qinker.oa.model.User;
import com.qinker.oa.model.vo.CustomerInfoVo;
import com.qinker.oa.service.CustomerInfoService;
import com.qinker.oa.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class CustomerExcelController {

    @Autowired
    private CustomerInfoService customerInfoService;

    @Autowired
    private UserService userService;

    @RequestMapping("/excel/downloadAll")
    public void downloadExcelAll(HttpSession session, HttpServletResponse response){
        try {
            User user = (User) session.getAttribute("user");
            if (user == null){
                response.sendRedirect("/login.html");
            }
            response.setHeader("Content-disposition", "attachment; filename="+new String("客人明细".getBytes("gb2312"),"ISO-8859-1")+".xlsx");
            response.setContentType("application/msexcel");// 定义输出类型
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("顾客名单");
            createHeader(sheet);
            List<CustomerInfoVo> customerInfoVos = new ArrayList<>();
            if (user.getRole().equalsIgnoreCase("user")){
                customerInfoVos= customerInfoService.getCustomerInfoListAllWithNoPage();
            }else {
                customerInfoVos = customerInfoService.getCustomerInfoListPersonWithNoPage(user.getId());
            }
            insertCellContent(customerInfoVos, sheet);

            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

    }

    @RequestMapping("/excel/downloadByUser")
    public void downloadExcelAllByUser(@RequestParam String nickname,HttpSession session, HttpServletResponse response){
        try{
            User user = (User) session.getAttribute("user");
            if (user == null){
                response.sendRedirect("/login.html");
            }
            response.setHeader("Content-disposition", "attachment; filename="+new String("客人明细".getBytes("gb2312"),"ISO-8859-1")+".xlsx");
            response.setContentType("application/msexcel");// 定义输出类型
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("顾客名单");
            createHeader(sheet);
            int ids[] = userService.getUserByName(nickname);
            if (ids != null && ids.length>0){
                List<CustomerInfoVo> infoVos = customerInfoService.downloadExcelAllByUser(null, null, ids);
                if (infoVos != null && infoVos.size()>0){
                    insertCellContent(infoVos, sheet);
                }
            }
            workbook.write(response.getOutputStream());
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }

    }

    public static void createHeader(Sheet sheet){
        Row row0 = sheet.createRow(0);
        createCell(row0);
        row0.getCell(0).setCellValue("序号");
        row0.getCell(1).setCellValue("顾客姓名");
        row0.getCell(2).setCellValue("电话号码");
        row0.getCell(3).setCellValue("所在地");
        row0.getCell(4).setCellValue("预算(万)");
        row0.getCell(5).setCellValue("客户等级");
        row0.getCell(6).setCellValue("联系状态");
        row0.getCell(7).setCellValue("客户来源");
        row0.getCell(8).setCellValue("置业顾问");
        row0.getCell(9).setCellValue("创建日期");
        row0.getCell(10).setCellValue("通话记录");
    }

    public static void createCell(Row row0){
        for (int i = 0;i< 11; i++){
            row0.createCell(i);

        }
    }

    public void insertCellContent(List<CustomerInfoVo> customerInfoVos, Sheet sheet){
        for (int i = 1; i < customerInfoVos.size()+1; i++){
            List<CustomerContactMessage> messages = customerInfoService.getMessageById(customerInfoVos.get(i-1).getId());
            String contactMessage = "";
            if (messages != null && messages.size()>0){
                for (CustomerContactMessage message : messages) {
                    contactMessage += message.getContactTime() + "\r\n"+ message.getContent() + "\r\n" + "--------------" + "\r\n";
                }
            }

            Row row = sheet.createRow(i);
            createCell(row);
            row.getCell(0).setCellValue(row.getRowNum());
            row.getCell(1).setCellValue(customerInfoVos.get(i-1).getCustomerName());
            row.getCell(2).setCellValue(customerInfoVos.get(i-1).getCustomerNumber());
            row.getCell(3).setCellValue(customerInfoVos.get(i-1).getCustomerArea());
            row.getCell(4).setCellValue(customerInfoVos.get(i-1).getBudget().toString());
            row.getCell(5).setCellValue(customerInfoVos.get(i-1).getCustomerLevel());
            row.getCell(6).setCellValue(customerInfoVos.get(i-1).getCustomerContact());
            row.getCell(7).setCellValue(customerInfoVos.get(i-1).getCustomerSource());
            row.getCell(8).setCellValue(customerInfoVos.get(i-1).getNickname());
            row.getCell(9).setCellValue(customerInfoVos.get(i-1).getCreateTime().toString());
            row.getCell(10).setCellValue(contactMessage);
        }
    }
}
